package com.zx.idc.backend.gui.api.management.basicdata.report.impl;

import com.zx.idc.backend.gui.api.commom.entity.UploadModel;
import com.zx.idc.backend.gui.api.commom.template.NIdcFtpTemplate;
import com.zx.idc.backend.gui.api.management.basicdata.BaseIdcMonitorDataService;
import com.zx.idc.backend.gui.api.management.basicdata.common.MonitorDefaultInstance;
import com.zx.idc.backend.gui.api.management.basicdata.common.MonitorService;
import com.zx.idc.backend.gui.api.management.basicdata.report.IdcBasicMonitorDataReportService;
import com.zx.idc.common.attributes.FTPReportDataTypeEnum;
import com.zx.idc.common.attributes.NetCommunicationFileTypeEnum;
import com.zx.idc.common.attributes.SchemaFileNameEnum;
import com.zx.idc.common.attributes.WebServiceInterfaceParamEnum;
import com.zx.idc.common.lang.ListUtil;
import com.zx.idc.ds.base.house.service.IBaseHouseInfoService;
import com.zx.idc.ds.base.oper.service.IBaseOperCompanyService;
import com.zx.idc.ds.common.constant.ReportStatusConstant;
import com.zx.idc.ds.slog.entity.SlogAbnormalIp;
import com.zx.idc.ds.slog.entity.SlogHotIp;
import com.zx.idc.ds.slog.entity.SlogIllegalSite;
import com.zx.idc.ds.slog.service.ISlogAbnormalIpService;
import com.zx.idc.ds.slog.service.ISlogHotIpService;
import com.zx.idc.ds.slog.service.ISlogIllegalSiteService;
import com.zx.idc.ds.sys.service.ISysConfigurationService;
import com.zx.idc.xml.entity.idcmonitor.IdcMonitor;
import com.zx.idc.xml.entity.idcmonitor.housemonitor.HouseMonitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author rxliuli
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class IdcBasicMonitorDataReportServiceImpl implements IdcBasicMonitorDataReportService {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private ISlogIllegalSiteService slogIllegalSiteService;
    @Autowired
    private ISlogAbnormalIpService slogAbnormalIpService;
    @Autowired
    private NIdcFtpTemplate nIdcFtpTemplate;
    @Autowired
    private MonitorService monitorService;
    @Autowired
    private MonitorDefaultInstance monitorDefaultInstance;
    @Autowired
    private BaseIdcMonitorDataService baseIdcMonitorDataService;
    @Autowired
    private ISysConfigurationService sysConfigurationService;

    @Autowired
    private IBaseHouseInfoService iBaseHouseInfoService;

    @Autowired
    private ISlogHotIpService iSlogHotIpService;

    @Autowired
    private IBaseOperCompanyService iBaseOperCompanyService;

    public void activeReportOld() {
        /*
        基础数据监测数据上报数据格式 idcMonitor
        机房监测数据 houseMonitor
        违法违规信息 illegalInfo
        ip 使用信息 ipInfo
         */
        //未上报的异常基础数据
        final List<SlogIllegalSite> slogIllegalSiteList = slogIllegalSiteService.selectList(new SlogIllegalSite().setReportStatus(ReportStatusConstant.NOT_REPORTED));
        String idcId = iBaseOperCompanyService.getIdcId();
        //异常 ip
        final IdcMonitor idcMonitor = new IdcMonitor(
            null,
            idcId,
            slogIllegalSiteList.stream()
                .map(monitorService::slogIllegalSiteTotHouseMonitor)
                .collect(Collectors.toList()), LocalDateTime.now());
        baseIdcMonitorDataService.update(idcMonitor, ReportStatusConstant.REPORTING);
        nIdcFtpTemplate.uploadXMLFile(new UploadModel<>(
            idcMonitor,
            SchemaFileNameEnum.BASE_DATA_EXCEPTION_DATA_REPORT,
            FTPReportDataTypeEnum.FTP_BASE_DATA_MONITORING_ABNORMAL_RECORDS,
            NetCommunicationFileTypeEnum.MONITOR_DATA
        ));
    }

    public void activeReportV2() {
        //获取，然后根据机房id分组
        List<SlogIllegalSite> slogIllegalSites = slogIllegalSiteService.selectList(
            new SlogIllegalSite().setReportStatus(ReportStatusConstant.NOT_REPORTED)
        );
        if (slogIllegalSites == null) {
            return;
        }
        Map<Long, List<SlogIllegalSite>> listMap = slogIllegalSites.stream().collect(Collectors.groupingBy(SlogIllegalSite::getId));

        //查出所有的机房
        List<Long> houseIds = iBaseHouseInfoService.selectListReturnId();
        List<HouseMonitor> houseMonitors = new ArrayList<>();
        for (Long houseId : houseIds) {
            List<SlogIllegalSite> reportSlogIllegalSites = listMap.get(houseId);
            if (ListUtil.isNotEmpty(reportSlogIllegalSites)) {
                List<HouseMonitor> houseMonitorList = reportSlogIllegalSites.stream().map(monitorService::slogIllegalSiteTotHouseMonitor).collect(Collectors.toList());
                houseMonitors.addAll(houseMonitorList);
            } else {
                //零上报
                HouseMonitor houseMonitor = monitorService.getZeroReportData(houseId);
                houseMonitors.add(houseMonitor);
            }
        }
        String idcId = iBaseOperCompanyService.getIdcId();
        final IdcMonitor idcMonitor = new IdcMonitor(
            null,
            idcId,
            houseMonitors,
            LocalDateTime.now()
        );

        baseIdcMonitorDataService.update(idcMonitor, ReportStatusConstant.REPORTING);
        nIdcFtpTemplate.uploadXMLFile(new UploadModel<>(
            idcMonitor,
            SchemaFileNameEnum.BASE_DATA_EXCEPTION_DATA_REPORT,
            FTPReportDataTypeEnum.FTP_BASE_DATA_MONITORING_ABNORMAL_RECORDS,
            NetCommunicationFileTypeEnum.MONITOR_DATA
        ));


    }

    @Override
    public void activeReport() {
        List<Long> houseIds = iBaseHouseInfoService.selectListReturnId();
        List<HouseMonitor> houseMonitors = new ArrayList<>();
        for (Long houseId : houseIds) {
            List<HouseMonitor> houseMonitorSubList = monitorService.queryBaseAbnormalData(houseId);
            if (houseMonitorSubList.size() == 0) {
                HouseMonitor houseMonitor = monitorService.getZeroReportData(houseId);
                houseMonitors.add(houseMonitor);
            }else{
                houseMonitors.addAll(houseMonitorSubList);
            }
        }

        nIdcFtpTemplate.uploadXMLFile(new UploadModel<>(
            getIdcMonitor(houseMonitors),
            SchemaFileNameEnum.BASE_DATA_EXCEPTION_DATA_REPORT,
            FTPReportDataTypeEnum.FTP_BASE_DATA_MONITORING_ABNORMAL_RECORDS,
            NetCommunicationFileTypeEnum.MONITOR_DATA
        ));
    }

    @Override
    public IdcMonitor slogIllegalSiteAddReport(Long id, String idcId) {
        final SlogAbnormalIp sis = slogAbnormalIpService.selectById(id);
        final HouseMonitor houseMonitor = monitorService.slogAbnormalIpTotHouseMonitor(sis);
        final IdcMonitor idcMonitor = monitorDefaultInstance.getIdcMonitor(idcId);
        idcMonitor.getHouseMonitor().add(houseMonitor);
        return idcMonitor;
    }

    private IdcMonitor getIdcMonitor(List<HouseMonitor> houseMonitors) {
        String idcId = iBaseOperCompanyService.getIdcId();
        return new IdcMonitor(
            null,
            idcId,
            houseMonitors,
            LocalDateTime.now()
        );
    }
}
